package cn.com.zetatech.maple.parser.utils;

import lombok.AccessLevel;
import lombok.NoArgsConstructor;

import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author cheng.cao@zetatech.com.cn
 */
@NoArgsConstructor(access = AccessLevel.PRIVATE)
public class ScriptEnginePool {

    private static final Map<String, ScriptEngine> scriptEngines = new ConcurrentHashMap<>();

    public static ScriptEngine getEngine(String language) {
        ScriptEngine engine = scriptEngines.get(language);
        if (engine == null) {
            synchronized (scriptEngines) {
                if (engine == null) {
                    ScriptEngineManager manager = new ScriptEngineManager();
                    engine = manager.getEngineByName(language);
                    scriptEngines.put(language, engine);
                }
            }
        }
        return engine;
    }

}